#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;
use File::Copy;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('qpdfjob');

open(F, ">auto-txt") or die;
print F "from file";
close(F);

my @bad_json = (
    "bare-option-false",
    "choice-mismatch",
    "encrypt-duplicate-key-length",
    "encrypt-missing-password",
    "encrypt-no-key-length",
    "pages-no-file",
    "overlay-no-file",
    "schema-error",
    "json-error"
    );
my @good_json = (
    ["choice-match", ""],
    ["input-file-password", "user"],
    ["empty-input", ""],
    ["replace-input", ""],
    ["encrypt-40", "u"],
    ["encrypt-128", "u"],
    ["encrypt-256-with-restrictions", "u"],
    ["add-attachments", ""],
    ["copy-attachments", ""],
    ["underlay-overlay", ""],
    ["underlay-overlay-password", ""],
    ["misc-options", ""],
    );
my $n_tests = 11 + scalar(@bad_json) + (2 * scalar(@good_json));


foreach my $i (@bad_json)
{
    $td->runtest("QPDFJob bad json: $i",
                 {$td->COMMAND => "qpdf --job-json-file=bad-json-$i.json"},
                 {$td->FILE => "bad-$i-json.out", $td->EXIT_STATUS => 2},
                 $td->NORMALIZE_NEWLINES);
}

foreach my $i (@good_json)
{
    my ($base, $pass) = @$i;
    if ($base eq 'replace-input')
    {
        copy("minimal.pdf", 'a.pdf');
    }
    $td->runtest("QPDFJob good json: $base",
                 {$td->COMMAND => "qpdf --job-json-file=job-json-$base.json"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    if ($base =~ m/encrypt-256/)
    {
        $td->runtest("check encryption $base",
                     {$td->COMMAND =>
                          "qpdf a.pdf --password=u" .
                          " --job-json-file=job-show-encryption.json"},
                     {$td->FILE => "job-json-$base.out", $td->EXIT_STATUS => 0},
                     $td->NORMALIZE_NEWLINES);
    }
    else
    {
        $td->runtest("check good json $base output",
                     {$td->COMMAND => "qpdf-test-compare a.pdf job-json-$base.pdf $pass"},
                     {$td->FILE => "job-json-$base.pdf", $td->EXIT_STATUS => 0});
    }
}


$td->runtest("QPDFJob json partial",
             {$td->COMMAND => "test_driver 83 - job-partial.json"},
             {$td->FILE => "job-partial-json.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("QPDFJob API",
             {$td->COMMAND => "test_driver 84 -",
                  $td->FILTER => "perl filter-progress.pl"},
             {$td->FILE => "job-api.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check output",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "test84.pdf"});
$td->runtest("json output from job",
             {$td->COMMAND => "qpdf --job-json-file=job-json-output.json"},
             {$td->FILE => "job-json-output.out.json", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

$td->runtest("C job API",
             {$td->COMMAND => "qpdfjob-ctest",
                  $td->FILTER => "perl filter-progress.pl"},
             {$td->FILE => "qpdfjob-ctest.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
foreach my $i (['a.pdf', 1], ['b.pdf', 2], ['c.pdf', 3], ['d.pdf', 4])
{
    $td->runtest("check output",
                 {$td->COMMAND => "qpdf-test-compare $i->[0] qpdfjob-ctest$i->[1].pdf"},
                 {$td->FILE => "qpdfjob-ctest$i->[1].pdf", $td->EXIT_STATUS => 0});
}
my $wide_out = `qpdfjob-ctest wide`;
$td->runtest("qpdfjob-ctest wide",
             {$td->STRING => "$?: $wide_out"},
             {$td->REGEXP => "0: (wide test passed|skipped wide)\n"},
             $td->NORMALIZE_NEWLINES);
if ($wide_out =~ m/skipped/)
{
    $td->runtest("skipped wide",
                 {$td->STRING => "yes"},
                 {$td->STRING => "yes"});
}
else
{
    $td->runtest("check output",
                 {$td->COMMAND => "qpdf-test-compare a.pdf qpdfjob-ctest-wide.pdf"},
                 {$td->FILE => "qpdfjob-ctest-wide.pdf", $td->EXIT_STATUS => 0});
}

cleanup();
$td->report($n_tests);
